package com.example.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.LauncherActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {

    public  static  final  String TAG="rate2";
    Handler handler;
    ListView listView;
    MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);


        //准备数据
        ArrayList listItems = new ArrayList<HashMap<String,String>>();
        for (int i=0;i<10;i++){
            HashMap<String,String> map=new HashMap<String,String>();
            map.put("ItemTitle","Rate:"+i);   //标题文字
            map.put("ItemDetail","detail:"+i);//详情描述
            listItems.add(map);
        }

        //生成是适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,
                listItems,//数据源
                R.layout.list_item,//布局
                new String[]{"ItemTitle","ItemDetail"},
                new int[] {R.id.title,R.id.detail}
        );

        //绑定控件
        listView=findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);

        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if(msg.what==5){
                    ArrayList<HashMap<String,String>> list= (ArrayList<HashMap<String, String>>) msg.obj;
//                    SimpleAdapter listItemAdapter = new SimpleAdapter(RateListActivity2.this,
//                            list,//数据源
//                            R.layout.list_item,//布局
//                            new String[]{"cname","cval"},
//                            new int[] {R.id.title,R.id.detail}
//                    );
//                    listView.setAdapter(listItemAdapter);
                    myAdapter=new MyAdapter(RateListActivity2.this,R.layout.list_item,list);
                    listView.setAdapter(myAdapter);
                }
                super.handleMessage(msg);
            }
        };
        //开启线程
        MyTask2 myTask= new MyTask2();
        myTask.setHandler(handler);
        Thread t = new Thread(myTask);
        t.start();//myTask.run();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        //获得当前点击行数据
        HashMap<String,String> map=(HashMap<String,String>)listView.getItemAtPosition(position);
        Log.i(TAG,"onItemClick:cname="+map.get("cname"));
        Log.i(TAG,"onItemClick:cval="+map.get("cval"));
        Log.i(TAG,"onItemClick:position="+position);

        Intent intent=new Intent(this,RateDetailActivity.class);
        intent.putExtra("cname",map.get("cname"));
        intent.putExtra("cval",Float.parseFloat(map.get("cval")));

        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        Log.i(TAG,"onItemLongCLick:pos="+position);
        //删除数据处理
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("提示")
                .setMessage("请确认是否删除该数据")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.i(TAG,":onclick:对话框事件处理");
//                        //删除数据项
//                        listView.remove(position);
//                        //更新适配器
//                        listItemAdaoter.notifyDataSetChanged();
                        myAdapter.remove(listView.getItemAtPosition(position));
                    }
        }).setNegativeButton("否",null);
        builder.create().show();

        return true;
    }
}